package com.euc.springbootframework.util;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.DateFormatConverter;

public class ExcelUtils<T> {
    public HSSFWorkbook exportExcel(String sheetName,String[] headers,String[] fields, Collection<T> dataset) {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setDefaultColumnWidth(15);
        HSSFCellStyle style = workbook.createCellStyle();

        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);

        HSSFCellStyle style2 = workbook.createCellStyle();
        HSSFFont font2 = workbook.createFont();
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        style2.setFont(font2);

        HSSFCellStyle dateStyle = workbook.createCellStyle();
        String excelFormatPattern = DateFormatConverter.convert(Locale.ENGLISH, "dd-MMMM-yy");
        HSSFDataFormat format = workbook.createDataFormat();
        dateStyle.setDataFormat(format.getFormat(excelFormatPattern));

        HSSFRow row = sheet.createRow(0);

        setHeader(headers,row,style);

        Iterator<T> it = dataset.iterator();
        int index = 0;
        while (it.hasNext()) {
            index++;
            row = sheet.createRow(index);
            T rowData = (T) it.next();
            setContent(headers, fields, dataset,workbook,row,style2,rowData,dateStyle);
        }
        return workbook;
    }

    private void setContent(String[] headers,String[] fields, Collection<T> dataset,HSSFWorkbook workbook,HSSFRow row,HSSFCellStyle style2,T rowData, HSSFCellStyle dateStyle){
        for (int i = 0; i < fields.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style2);
            String fieldName = fields[i];
            Object value = null;
            try {
                value = BeanUtils.getProperty(rowData, fieldName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            changeValueType(value, cell,workbook,dateStyle);
        }
    }

    private void setHeader(String[] headers,HSSFRow row,HSSFCellStyle style){
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
    }

    private void changeValueType(Object value,HSSFCell cell,HSSFWorkbook workbook, HSSFCellStyle dateStyle) {
        String textValue = null;
        if (value instanceof Integer) {
            int intValue = (Integer) value;
            cell.setCellValue(intValue);
        } else if (value instanceof Float) {
            float fValue = (Float) value;
            //textValue = Float.toString(fValue);
            cell.setCellValue(fValue);
        } else if (value instanceof Double) {
            double dValue = (Double) value;
            //textValue = Double.toString(dValue);
            cell.setCellValue(dValue);
        } else if (value instanceof Long) {
            long longValue = (Long) value;
            cell.setCellValue(longValue);
        } else if (value instanceof Date) {
            Date dateValue = (Date) value;
            cell.setCellValue(dateValue);
            cell.setCellStyle(dateStyle);
        } else {
            textValue = value.toString();
            cell.setCellValue(textValue);
        }
    }
}
